function ScnMapTopTenAssistant(argFromPusher, tp) {
	this.args = argFromPusher;
	this.tecPlates = tp;
	this.service = "";
}

ScnMapTopTenAssistant.prototype = {
	setup: function() {
		Ares.setupSceneAssistant(this);
		this._mapTypeMenuModel = {
			toggleCmd:'back',
			items: [
				{label: $L('Terrain'), command:'do-terr'},
				{label: $L('Hybrid'), command:'do-hyb'},
				{label: $L('Satellite'), command:'do-sat'},
				{label: $L('Roadmap'), command:'do-map'}
			]
		};
		this._mapShareMenuModel = {
			toggleCmd:'back',
			items: [
				{label: $L('Email'), command:'do-mail'},
				{label: $L('Messaging'), command:'do-msg'},
				{label: $L('Twitter'), command:'do-twit'},
				{label: $L('Facebook'), command:'do-fb-app'}
			]
		};
		if (this.args.length == 1) {
			this.cmdMenuModel = {
				items: [ 
						{label: $L('Minus'), command:'do-zoom-out', iconPath: "images/zoom_out4.png"},
						{label: "X", items: [
								{label: $L('Map type'), submenu:'_mapTypeMenu'},
								{label: $L('Share it'), submenu:'_mapShareMenu'} //, icon: "send"}
							]
						},
						{label: $L('Plus'), command:'do-zoom-in', iconPath: "images/zoom_in4.png"}
				]
			};
		}
		else {
			this.cmdMenuModel = {
				items: [ 
						{label: $L('Minus'), command:'do-zoom-out', iconPath: "images/zoom_out4.png"},
						{label: "X", items: [
								{label: $L('Map Type'), submenu:'_mapTypeMenu'}
							]
						},
						{label: $L('Plus'), command:'do-zoom-in', iconPath: "images/zoom_in4.png"}
				]
			};
		}
//		this.controller.setupWidget(Mojo.Menu.commandMenu, this.attributes = {menuClass: 'no-fade'}, this.cmdMenuModel);
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuModel);
		this.controller.setupWidget('_mapTypeMenu', undefined, this._mapTypeMenuModel);
		this.controller.setupWidget('_mapShareMenu', undefined, this._mapShareMenuModel);
		
		try {
			this.infoBubbles = [];
			this.markers = [];
			this.mp = this.$.map1.map;
			if (this.tecPlates) {
				var kmlOptions = {
					preserveViewport: true
				};
				var geoxml = new google.maps.KmlLayer("http://www.appswerkz.com/seismix/seismix.kmz", kmlOptions);
//				var geoxml = new google.maps.KmlLayer("http://www.appswerkz.com/seismix/seismix-red.kmz", kmlOptions);
//				var geoxml = new google.maps.KmlLayer("http://www.appswerkz.com/seismix/seismix-color.kmz", kmlOptions);
//				var geoxml = new google.maps.KmlLayer("http://www.geocodezip.com/Earths_Tectonic_PlatesD.xml", kmlOptions);
				geoxml.setMap(this.mp);
			}
			this.mp.setCenter( new google.maps.LatLng(0,0));
			this.bounds = new google.maps.LatLngBounds();
			for (m=0; m<this.args.length; m++){
				this.drawMarker(this.args[m]);
			} //end for
		}
		catch (err){
			myInfo("Error: ", err);
		}
		if (this.args.length == 1) {
			this.mp.setZoom(6);
		}
		else {
			this.mp.fitBounds(this.bounds);
		}
		this.mp.setCenter(this.bounds.getCenter());
	}, //end setup
	
	drawMarker: function(eq){
		var imgPath = "";
		var latlon = new google.maps.LatLng(eq.lat, eq.lon);
		this.bounds.extend(latlon);
		if (eq.mag <= 5.0) {
			imgPath = "images/markers/marker-blue.png";
		}
		else if (eq.mag <= 6.0) {
			imgPath = "images/markers/marker-orange.png";
		}
		else if (eq.mag > 6.0) {
			imgPath = "images/markers/marker-red.png";
		}
		var myMarker = new google.maps.Marker({
			map: this.mp,
			position : latlon,
			icon : new google.maps.MarkerImage(imgPath,
				new google.maps.Size(32, 39),
				new google.maps.Point(0,0),
				new google.maps.Point(7, 36)),
			zIndex : (Number(eq.mag) * 10)
		}); //end myMarker
		
		var cont = "<DIV class='bubble-l'>" + eq.flynn_region + "</DIV>";
		cont = cont + "<DIV class='bubble-m'>"+ $L("Magnitude") + ":&nbsp;" + eq.magString + "</DIV>";
		cont = cont + "<DIV class='bubble-s'>"+ $L("Depth") + ":&nbsp;" + eq.depthString + ", " + $L("Dist") + ":&nbsp;" + eq.distString + ",</DIV>";
		cont = cont + "<DIV class='bubble-s'>"+ $L("Date/Time (UTC)") + ":&nbsp;" + eq.datetime;
		var iw = new google.maps.InfoWindow({content: cont, position: latlon, disableAutoPan: false});
		
/*
		var cont = eq.magString + " - " + eq.flynn_region;
		var iw = new InfoBubble({
			map: this.mp,
			content: '<div id="bubble" class="phoneytext2">' + cont + '</div>',
			shadowStyle: 1,
			padding: 0,
			borderWidth: 1,
			backgroundColor: 'rgb(57,57,57)',
			borderRadius: 4,
			arrowSize: 10,
			borderColor: '#2c2c2c',
			disableAutoPan: true,
			hideCloseButton: true,
//			arrowPosition: 25,
			arrowStyle: 0,
			backgroundClassName: 'phoney',
			backgroundClassNameClicked: 'phoney-clicked'
		}); //end InfoBubble
*/		
		google.maps.event.addListener(myMarker,"click",(function(){
			this.hideInfoBubbles();
			iw.open(this.mp, myMarker);
		}).bind(this));
		this.infoBubbles.push(iw);
		if (this.args.length == 1) {
			iw.open(this.mp, myMarker);
		}
	}, // end drawMarker()
	
	hideInfoBubbles: function(){
		for (b=0; b<this.infoBubbles.length; b++){
			this.infoBubbles[b].close();
		}
	}, //end hideInfoBubbles

	handleCommand: function(event) {
		if (event.type === Mojo.Event.command) {
			if (event.command == 'do-zoom-out') { 
				if (this.mp.getZoom() >= 1) {this.mp.setZoom(this.mp.getZoom() - 1);}
			}
			if (event.command == 'do-zoom-in') { 
				this.mp.setZoom(this.mp.getZoom() + 1);
			}
			if (event.command == 'do-map') { 
				this.mp.setMapTypeId(google.maps.MapTypeId.ROADMAP);
			}
			if (event.command == 'do-hyb') { 
				this.mp.setMapTypeId(google.maps.MapTypeId.HYBRID);
			}
			if (event.command == 'do-sat') { 
				this.mp.setMapTypeId(google.maps.MapTypeId.SATELLITE);
			}
			if (event.command == 'do-terr') { 
				this.mp.setMapTypeId(google.maps.MapTypeId.TERRAIN);
			}
			if (event.command == 'do-fb') { 
				this.selectService("facebook");
			}
			if (event.command == 'do-fb-app') { 
				this.selectService("facebook-app");
			}
			if (event.command == 'do-twit') { 
				this.selectService("twitter-app");
			}
			if (event.command == 'do-mail') { 
				this.selectService("mail");
			}
			if (event.command == 'do-msg') { 
				this.selectService("message");
			}
		}
	},
	
	makeTinyUrl: function(longUrl, secondTry) {
		if (this.$.wsTinyURL.isReady()){
			if (secondTry) {
				url = "http://b.tinyurl.com/api-create.php/";
			}
			var params = {
				url: longUrl
			};
			this.$.wsTinyURL.setParameters(params);
			this.$.wsTinyURL.execute();
		}
	},
	
	wsTinyURLSuccess: function(inSender, inResponse, inRequest) {
		if (inResponse) {
			this.runService(inResponse);
		}
	},
	
	wsTinyURLFailure: function(inSender, inError) {
		Mojo.Log.error("wsTinyURLFailure:", inError);
		//trigger the second try for creating a tinyURL with their backup service
		if (inSender.url != "http://tinyurl.com/api-create.php") {
			makeTinyUrl(inSender.parameters.url, true);
		}
	},
	
//http://maps.google.com/?q=Earthquake+of+Magnitude%3A+5.2+Where%3A+Northern+Mid-Atlantic+Ridge+When%3A+2011-05-06T16%3A20%3A39Z@49.614981,14.454345&z=6&t=t
	
	selectService: function(svc) {
		this.service = svc;
		this.txtShort = $L("Earthquake of magnitude") + " " + this.args[0].magString + " " + $L("Where") + ": " + this.args[0].flynn_region + " " + $L("When") + ": " + this.args[0].datetime;
		this.txtShortEnc = this.txtShort.split(' ').join('+');
		var urlGMaps = "http://maps.google.com/?z=6&t=t&iwd=1&q=";
		var urlGMapsEnc  = encodeURIComponent(urlGMaps);
		var urlGMapsComplete = urlGMaps + this.args[0].lat + "," + this.args[0].lon + "(" + this.txtShort + ")" ;
		this.makeTinyUrl(urlGMapsComplete, false);
	},
	
	runService: function(tinyUrl) {
		var tinyUrlEnc = encodeURI(tinyUrl);
		var txtLong = $L("Earthquake of magnitude") + " " + this.args[0].magString + " " + $L("occurred at") + " " + this.args[0].flynn_region + " " + $L("on") + " " + this.args[0].datetime + ". " + $L("Click this") + ": " + tinyUrl;
		try {
			switch(this.service) {
				case "facebook-app":
					myInfo("Share via FB-app", txtLong);
					var request = new Mojo.Service.Request("palm://com.palm.applicationManager", {
						method: "launch",
						parameters: {
							id: 'com.palm.app.facebook',
							params: {
									status: txtLong
							}
						},
						onFailure: function() {
							myInfo('Failed to launch with Facebook-App');
							//run the facebook-web option
							this.selectService("facebook");
						}.bind(this)
					});
					break;
				case "facebook":
					var fbTitle = this.txtShortEnc;
					var fbDescr = encodeURIComponent(txtLong);
					var fbURL = tinyUrl;
					var fbImage = encodeURI("http://appswerkz.com/seismix/SeismiX-Icon-48x48.png");
					var urlFb = "http://www.facebook.com/sharer.php?s=100&p[title]=" + fbTitle + "&p[summary]=" + fbDescr + "&p[url]=" + fbURL + "&p[images][0]=" + fbImage;
					//var urlFb = "http://www.facebook.com/sharer.php?t=" + fbTitle + "&u=" + fbURL;
					myInfo("Share via FB", urlFb);
					this.controller.serviceRequest("palm://com.palm.applicationManager", {
						method: "open",
						parameters: {
							id: 'com.palm.app.browser',
							params: {
									target: urlFb
							}
						}
					});
					break;
				case "twitter":
					//see http://dev.twitter.com/doc/post/statuses/update
					var urlTw = "http://m.twitter.com/home?status=" + this.txtShortEnc + "+" + tinyUrl;
					myInfo("Share via Tw", urlTw);
					this.controller.serviceRequest("palm://com.palm.applicationManager", {
						method: "open",
						parameters: {
							id: 'com.palm.app.browser',
							params: {
									target: urlTw
							}
						}
					});
					break;
					
				case "twitter-app":
					var appids = ['com.funkatron.app.spaz', 'com.funkatron.app.spaz-sped', 'com.dotsandlines.carbon'], index = 0;
					var p = {};
					function makeCall(txt) {
						if (index < appids.length) {
							Mojo.Log.info('Trying to launch with appid %s', appids[index]);
							if (appids[index] === "com.funkatron.app.spaz") {
								p = {action: 'post', msg: txt + "+" + tinyUrl};
							}
							if (appids[index] === "com.funkatron.app.spaz-sped") {
								p = {action: 'post', msg: txt + "+" + tinyUrl};
							}
							if (appids[index] === "com.dotsandlines.carbon") {
								p = {action: 'compose', body: txt + "+" + tinyUrl};
							}
							var request = new Mojo.Service.Request("palm://com.palm.applicationManager", {
								method: 'launch',
								parameters: {
									id: appids[index],
									params: p
								},
								onFailure: function() {
									Mojo.Log.info('Failed to launch with appid %s', appids[index]);
									index++; // go to next appid
									makeCall(); // retry
								}.bind(this) //end onFailure
							}); //end request
						}	//end if
						else {
							myInfo('Failed to launch with Twitter-App');
							//run the twitter-web option
							//this.selectService("twitter");
							return false;
						} // end else
						return true;
					}	//end function makeCall
					if (!makeCall(this.txtShort)) {
						myInfo("!makeCall");
						this.selectService("twitter");
					}
					break;
					
				case "mail":
					myInfo("Share via Message", this.txtShort, txtLong);
					this.controller.serviceRequest('palm://com.palm.applicationManager', {
						method:'open',
						parameters: {
							id: 'com.palm.app.email', 
							params: {
								summary: this.txtShort,
								text: txtLong
							}
						}
					});
					break;
				case "message":
					myInfo("Share via Message", this.txtShort);
					this.controller.serviceRequest('palm://com.palm.applicationManager', {
						method:'launch',
						parameters: {
							id: 'com.palm.app.messaging', 
							params: {
								messageText: this.txtShort + " " + tinyUrl
							}
						}
					});
					break;
			} // end switch
		} // end try
		catch(err) {
			Mojo.Log.error("Could not send Share", err);
		}
	},
	
	cleanup: function() {
		Ares.cleanupSceneAssistant(this);
	} //end cleanup,
};